import Mock from "mockjs";
import qs from "qs";

Mock.setup({
  //模拟随机网络延迟
  timeout: "500-1000",
});

// 拦截请求，
// 第一个参数：url，使用正则去匹配
// 第二个参数：请求方式
// 第三个参数： 生成数据的函数
// Mock.mock(/\/my\/test/, "get", () => {
//   //模拟数据逻辑
//   const arr = [];
//   for (let i = 0; i < 5; i++) {
//     arr.push(
//       Mock.mock({
//         id: "@id",
//         name: "@name",
//       })
//     );
//   }
//   return { msg: "请求测试接口成功", result: arr };
// });

//模拟收藏
Mock.mock(/\/member\/collect/, "get", (config) => {
  //模拟数据逻辑
  const items = [];
  const queryString = config.url.split("?")[1];
  const queryObject = qs.parse(queryString);
  for (let i = 0; i < queryObject.pageSize; i++) {
    items.push(
      Mock.mock({
        id: "@id",
        name: "@ctitle(10,20)",
        desc: "@ctitle(4,10)",
        price: "@float(100,200,2,2)",
        picture: `http://zhoushugang.gitee.io/erabbit-client-pc-static/uploads/clothes_goods_${Mock.mock(
          "@integer(1,8)"
        )}.jpg`,
      })
    );
  }
  return {
    msg: "请求测试接口成功",
    result: {
      items,
      counts: 35,
      //+号为了转类型
      pageSize: +queryObject.pageSize,
      page: +queryObject.page,
    },
  };
});

//模拟足迹
Mock.mock(/\/member\/foot/, "get", (config) => {
  //模拟数据逻辑
  const items = [];
  const queryString = config.url.split("?")[1];
  const queryObject = qs.parse(queryString);
  for (let i = 0; i < queryObject.pageSize; i++) {
    items.push(
      Mock.mock({
        id: "@id",
        name: "@ctitle(10,20)",
        desc: "@ctitle(4,10)",
        price: "@float(100,200,2,2)",
        picture: `http://zhoushugang.gitee.io/erabbit-client-pc-static/uploads/clothes_goods_${Mock.mock(
          "@integer(1,8)"
        )}.jpg`,
      })
    );
  }
  return {
    msg: "请求测试接口成功",
    result: {
      items,
      counts: 35,
      //+号为了转类型
      pageSize: +queryObject.pageSize,
      page: +queryObject.page,
    },
  };
});